﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Einstein.Data;

namespace Einstein.Group.DAL
{
    public class RoleInfoDAL
    {

        public IList<RoleInfo> SelectAll()
        {
            using (EinsteinEntities entities = new EinsteinEntities())
            {
                return entities.RoleInfo.ToList<RoleInfo>();
            }
        }

        public bool Add(RoleInfo info)
        {
            using (EinsteinEntities entities = new EinsteinEntities())
            {
                entities.RoleInfo.AddObject(info);
                return entities.SaveChanges() > 0;
            }
        }

        public bool Exist(RoleInfo info)
        {
            using (EinsteinEntities entities = new EinsteinEntities())
            {
                return entities.RoleInfo.Count(a => a.RoleID != info.RoleID && a.RoleName == info.RoleName) > 0;
            }
        }

        public bool Update(RoleInfo info)
        {

            using (EinsteinEntities entities = new EinsteinEntities())
            {
                foreach (var item in entities.RoleInfo.Where(a => a.RoleID == info.RoleID))
                {
                    item.RoleName = info.RoleName;
                    item.Desc = info.Desc;
                    item.UpdateTime = info.UpdateTime;
                    item.UpdatePerson = info.UpdatePerson;
                }
                return entities.SaveChanges() > 0;
            }
        }

        public bool Delete(RoleInfo info)
        {
            using (EinsteinEntities context = new EinsteinEntities())
            {
                foreach (var item in context.RoleInfo.Where(a => a.RoleID == info.RoleID))
                {
                    context.DeleteObject(item);
                }
                return context.SaveChanges() > 0;
            }
        }
    }
}
